package com.tro.mail.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tro.mail.dao.entity.MailMarkMail;
import com.tro.mail.dao.entity.MailMarker;
import com.tro.mail.dao.entity.MailMarkerExample;
import com.tro.mail.dao.mapper.MailMarkMailMapper;
import com.tro.mail.dao.mapper.MailMarkerMapper;
import com.tro.mail.service.MailMarkerManager;
@Service
public class MailMarkerManagerImp implements MailMarkerManager{
	@Autowired
	MailMarkerMapper mapper;
	
	@Override
	public int countByExample(MailMarkerExample example) {
		
		return mapper.countByExample(example);
	}

	@Override
	public int deleteByExample(MailMarkerExample example) {
		
		return mapper.deleteByExample(example);
	}
	@Autowired
	MailMarkMailMapper markMailMapper;
	@Transactional("mailTran")
	@Override
	public int insert(MailMarker record) {
		mapper.insert(record);
		MailMarkMail markMail=new MailMarkMail();
		if(StringUtils.isNotBlank(record.getMarkMails())){
			String[] ids=record.getMarkMails().split(",");
			for(String id:ids){
				if(StringUtils.isNotBlank(id)){
					markMail=new MailMarkMail();
					markMail.setMailId(Long.parseLong(id));
					markMail.setMarkId(record.getId());
					markMail.setMailType(record.getType());
					markMailMapper.insert(markMail);
				}
			}
		}
		return 1;
	}

	@Override
	public int insertSelective(MailMarker record) {
		
		return mapper.insertSelective(record);
	}

	@Override
	public List<MailMarker> selectByExample(MailMarkerExample example) {
		
		return mapper.selectByExample(example);
	}

	@Override
	public int updateByExampleSelective(MailMarker record, MailMarkerExample example) {
		
		return mapper.updateByExampleSelective(record, example);
	}

	@Override
	public int updateByExample(MailMarker record, MailMarkerExample example) {
		
		return mapper.updateByExample(record, example);
	}
	@Transactional("mailTran")
	@Override
	public void addMark(MailMarker record) {
		MailMarkMail markMail=new MailMarkMail();
		if(StringUtils.isNotBlank(record.getMarkMails())){
			String[] ids=record.getMarkMails().split(",");
			for(String id:ids){
				if(StringUtils.isNotBlank(id)){
					markMail=new MailMarkMail();
					markMail.setMailId(Long.parseLong(id));
					markMail.setMarkId(record.getId());
					markMail.setMailType(record.getType());
					markMailMapper.insert(markMail);
				}
			}
		}
		
	}
	
	public List<MailMarker> selectMarkByRelation(Long mail_id){
		return mapper.selectMarkByRelation(mail_id);
	}

}
